<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>在数组中搜索给定的值，如果成功则返回首个相应的键名</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-search" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_search</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_search</span> &mdash; <span class="dc-title">
     在数组中搜索给定的值，如果成功则返回首个相应的键名
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-search-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>array_search</strong></span>
       ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span>
      , <span class="methodparam"><span class="type">array</span> <code class="parameter">$haystack</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$strict</code><span class="initializer"> = false</span></span>
     ] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

    <p class="para rdfs-comment">
     大海捞针，在大海（<code class="parameter">haystack</code>）中搜索针（
     <code class="parameter">needle</code> 参数）。
    </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-search-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">needle</code></dt>

     <dd>

      <p class="para">
       搜索的值。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        如果 <code class="parameter">needle</code> 是字符串，则比较以区分大小写的方式进行。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<code class="parameter">haystack</code></dt>

     <dd>

      <p class="para">
       这个数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">strict</code></dt>

     <dd>

      <p class="para">
       如果可选的第三个参数 <code class="parameter">strict</code> 为 <strong><code>TRUE</code></strong>，则
     <span class="function"><strong>array_search()</strong></span> 将在 <code class="parameter">haystack</code>
     中检查<em class="emphasis">完全相同</em>的元素。
       这意味着同样严格比较 <code class="parameter">haystack</code> 里 <code class="parameter">needle</code> 的 <a href="language.types.html" class="link">类型</a>，并且对象需是同一个实例。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-search-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果找到了 <code class="parameter">needle</code> 则返回它的键，否则返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   如果 <code class="parameter">needle</code> 在
     <code class="parameter">haystack</code>
     中出现不止一次，则返回第一个匹配的键。要返回所有匹配值的键，应该用
     <span class="function"><a href="array_keys.html" class="function">array_keys()</a></span> 加上可选参数
     <code class="parameter">search_value</code> 来代替。
  </p>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">此函数可能返回布尔值
<strong><code>FALSE</code></strong>，但也可能返回等同于 <strong><code>FALSE</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-search-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        As with all internal PHP functions as of 5.3.0,
        <span class="function"><strong>array_search()</strong></span> returns <strong><code>NULL</code></strong> if invalid parameters
        are passed to it.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

  <div class="refsect1 examples" id="refsect1-function.array-search-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6064">
    <p><strong>Example #1 <span class="function"><strong>array_search()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'blue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;2;<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;1;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-search-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_keys.html" class="function" rel="rdfs-seeAlso">array_keys()</a> - 返回数组中部分的或所有的键名</span></li>
    <li class="member"><span class="function"><a href="array_values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - 返回数组中所有的值</span></li>
    <li class="member"><span class="function"><a href="array_key_exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - 检查数组里是否有指定的键名或索引</span></li>
    <li class="member"><span class="function"><a href="in_array.html" class="function" rel="rdfs-seeAlso">in_array()</a> - 检查数组中是否存在某个值</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>